package cn.iocoder.yudao.module.blog.controller.app.space;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.blog.controller.admin.spacelike.vo.BlogSpaceLikeCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.spacelike.vo.BlogSpaceLikePageReqVO;
import cn.iocoder.yudao.module.blog.controller.app.space.likevo.AppBlogSpaceLikeListReqVO;
import cn.iocoder.yudao.module.blog.controller.app.space.likevo.AppBlogSpaceLikeRespVO;
import cn.iocoder.yudao.module.blog.convert.spacelike.BlogSpaceLikeConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.spacelike.BlogSpaceLikeDO;
import cn.iocoder.yudao.module.blog.service.space.BlogSpaceService;
import cn.iocoder.yudao.module.blog.service.spacelike.BlogSpaceLikeService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 APP - 点赞")
@RestController
@RequestMapping("/blog/space/like")
@Validated
public class AppBlogSpaceLikeController {
    @Resource
    private BlogSpaceService blogSpaceService;

    @Resource
    private BlogSpaceLikeService blogSpaceLikeService;

    @Resource
    private MemberUserApi memberUserApi;


    @PostMapping("/create")
    @Operation(summary = "点赞")
    public CommonResult<Long> createLike(@Valid @RequestBody BlogSpaceLikeCreateReqVO createReqVO) {
        Long id = blogSpaceLikeService.createSpaceLike(createReqVO);
        blogSpaceService.updateSpaceLikeNum(createReqVO.getSpaceId(), 1);
        return success(createReqVO.getSpaceId());
    }

    @GetMapping("/page")
    @Operation(summary = "获得点赞分页")
    public CommonResult<PageResult<AppBlogSpaceLikeRespVO>> getLikePage(@Valid BlogSpaceLikePageReqVO pageVO) {
        PageResult<BlogSpaceLikeDO> pageResult = blogSpaceLikeService.getSpaceLikePage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), BlogSpaceLikeDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        return success(BlogSpaceLikeConvert.INSTANCE.convertAppPage(pageResult, memberMap));
    }

    @PostMapping("/list")
    @Operation(summary = "获得文章点赞列表")
    public CommonResult<List<Long>> getLikeList(@Valid @RequestBody AppBlogSpaceLikeListReqVO reqVO){
        List<BlogSpaceLikeDO> list = blogSpaceLikeService.getSpaceLikeList(reqVO.getMemberId(), reqVO.getSpaceList());
        return success(convertList(list, BlogSpaceLikeDO::getSpaceId));
    }
}
